<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form :inline="true" :model="data">
          <div class="box">
            <div class="left">
              <el-form-item label="标签名称">
                <el-input v-model="data.tagName" placeholder="请输入标签名称" />
              </el-form-item>
              <el-form-item label="状态">

                <el-select v-model="data.state" placeholder="请选择">

                  <el-option label="启用" value="1" />
                  <el-option label="禁用" value="0" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" @click="clearSubmit">清除</el-button>
                <el-button type="primary" size="small" @click="onSubmit">搜索</el-button>
              </el-form-item>
            </div>

            <div class="right">
              <el-form-item v-show="subjectid?true:false">
                <el-button type="text" icon="el-icon-back" @click="$router.back() ">
                  返回学科</el-button>

              </el-form-item>
              <el-form-item>
                <el-button type="success" size="small" @click="addtages">
                  <i class="el-icon-edit" />
                  <span>新增标签</span>
                </el-button>
              </el-form-item>

            </div>
          </div>
        </el-form>
        <!-- 头部一共多少条数据 -->
        <el-alert
          :title="`数据一共${counts}条`"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        />
        <!-- 表格内容 -->
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          element-loading-text="给我一点时间"
          fit
          highlight-current-row
          style="width: 100%;padding-top:15px;"
        >
          <el-table-column align="center" :label="$t('table.id')" type="index" />
          <el-table-column align="center" prop="subjectName" label="所属学科">
            <template slot-scope="scope">
              <span>{{ scope.row.subjectName }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="tagName" label="标签名称">
            <template slot-scope="scope">
              <span>{{ scope.row.tagName }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="username" label="创建者">
            <template slot-scope="scope">
              <span>{{ scope.row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="addDate" label="创建日期">
            <template slot-scope="scope">
              <span>{{ scope.row.addDate |formatDate('yyyy-MM-dd hh:mm:ss') }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="state" label="状态">
            <template slot-scope="scope">
              <span>{{ scope.row.state?'已启用':'已禁用' }}</span>
            </template>
          </el-table-column>
          <!-- 操作按钮 -->
          <el-table-column
            label="操作"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="changeState(scope.row.id,scope.row.state)"
              >{{ scope.row.state?'禁用':'启用' }}</el-button>
              <el-button
                size="mini"
                type="text"
                :disabled="scope.row.state?false:true"
                @click="editTage(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                :disabled="scope.row.state?false:true"
                @click="removeTage(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row v-show="counts> page.pagesize?true:false" type="flex" justify="end" style="margin-top:20px;">
          <PageTool
            :pagination-page="page.page"
            :pagination-pagesize="page.pagesize"
            :total="counts"
            @pageChange="handleCurrentChange"
            @pageSizeChange="handleSizeChange"
          />
        </el-row>
      </el-card>
      <TagsAdd ref="tagAdd" :show-tagedialog.sync="showTagedialog" :symple-date="sympleDate" :subjectid="subjectid" @addSub="addSub" />
    </div>
  </div>

</template>

<script>
import { list, remove, add, update, changeState } from '@/api/hmmm/tags'
import PageTool from './../components/pageTool'
import TagsAdd from './../components/tags-add.vue'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  components: {
    PageTool,
    TagsAdd
  },
  data() {
    return {
      tableKey: 0,
      listLoading: true,
      showTagedialog: false,
      sympleDate: [],
      list: [],
      id: null,
      data: {
        tagName: '',
        state: ''
      },
      page: {
        page: 1,
        pagesize: 10
      },
      counts: 0,
      newobj: {},
      subjectid: '' // 判断是否是从学科跳转过来的
    }
  },
  created() {
    this.subjectid = this.$route.query.id
    this.getlist()
    this.getSimple()
  },
  methods: {
    async getlist() {
      this.listLoading = true
      if (this.subjectid !== undefined) {
        this.page.subjectID = this.subjectid
      }
      list(this.page).then(data => {
        this.list = data.data.items
        this.counts = data.data.counts
        this.listLoading = false
      })
        .catch(e => {
          this.$message('获取列表失败')
        })
      this.listLoading = false
    },

    // 搜索
    onSubmit() {
      for (const key in this.data) {
        if (this.data[key] !== '') {
          this.newobj[key] = this.data[key]
        }
      }
      if (this.subjectid !== undefined) {
        this.page.subjectID = this.subjectid
      }
      list({ ...this.page, ... this.newobj }).then(data => {
        this.list = data.data.items
        this.counts = data.data.counts
        this.listLoading = false
      })
     this.newobj = {}
    },
    // 清除
    clearSubmit() {
      this.data = {
        tagName: '',
        state: ''
      }
      this.page = {
        page: 1,
        pagesize: 10
      }
      this.newObj = {}
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.page.page = val
      for (const key in this.data) {
        if (this.data[key] !== '') {
          this.newobj[key] = this.data[key]
        }
         }
      list({ ...this.page, ... this.newobj }).then(data => {
        this.list = data.data.items
        this.counts = data.data.counts
        this.listLoading = false
      })
      this.page.page = 1
     this.newobj = {}
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.page.pagesize = val
      this.getlist()
    },
    // 启动按钮,改变状态
    async changeState(id, state) {
      if (state === 1) {
        await changeState({ id, state: 0 })
      } else if (state === 0) {
        await changeState({ id, state: 1 })
      }
      this.$message.success('修改状态操作成功')
      this.onSubmit()
    },
    // 删除标签按钮
    removeTage(id) {
      this.$confirm('你确定要删除吗？').then(() => {
        remove({ id: id }).then(data => {
          this.$message.success('删除标签成功')
          this.page.page = Math.ceil((this.counts - 1) / this.page.pagesize)
          this.getlist()
        }).catch(e => {
          this.$message.e('删除标签失败')
        })
      }).catch(() => {
        this.$message('取消操作')
      })
    },
    // 获取弹窗的学科列表数据
    async getSimple() {
      const { data } = await simple()
      this.sympleDate = data
    },
    // 弹窗确认按钮
    async addSub(formdata) {
      // 如果没有id就是新增，有就是修改
      if (!this.id) {
        await add(formdata)
        this.$message.success('添加标签成功')
        this.getlist()
      } else {
        formdata.id = this.id
        await update(formdata)
        this.$message.success('修改标签成功')
        this.getlist()
      }
      this.showTagedialog = false
      this.id = null
    },

    // 修改按钮 打开弹窗
    editTage(data) {
      this.showTagedialog = true
      this.$refs.tagAdd.text = '修改'
      if (this.subjectid !== undefined) {
        this.$refs.tagAdd.formdata.subjectID = Number(this.subjectid)
      }
      this.$refs.tagAdd.formdata.subjectID = data.subjectID
      this.$refs.tagAdd.formdata.tagName = data.tagName
      this.id = data.id
    },
    // 新增标签 打开弹窗
    addtages() {
      if (this.subjectid !== undefined) {
        this.$refs.tagAdd.formdata.subjectID = Number(this.subjectid)
      }
      this.showTagedialog = true
      this.$refs.tagAdd.text = '新增'
    }
  }
}

</script>

<style rel="stylesheet/scss" scoped lang='scss'>
.box{
  display: flex;
  justify-content: space-between;
}

</style>
